package node;

import java.io.BufferedWriter;
import java.util.Random;

public class NodeExecuteThread extends Thread{
	
	private BufferedWriter pw;
	private String msg;
	private NodeThread nodeThread;
	Random rd = new Random();
	private int port;

	public NodeExecuteThread(NodeThread nodeThread,BufferedWriter pw,int port,String msg) {
		this.pw  = pw;
		this.port = port;
		this.msg = msg;
		this.nodeThread = nodeThread;
	}


	public void run() {
		nodeThread.executingCount++;
		System.out.println("-----------------Node "+port+" Task start------------------");
		try {
			Thread.sleep(rd.nextInt(1000));
			nodeThread.executingCount--;
			pw.write(msg);
			pw.flush();
		} catch (Exception e) {
			e.printStackTrace();
			nodeThread.executingCount--;
		} 
		System.out.println("-----------------Node "+port+" Task end------------------");
		
	}
	
}
